﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Web;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using AcctrueWMS.Foundation.Common.Exception;

namespace AcctrueWMS.Foundation.Data
{
    class ClsAliasManager : IClsAliasManager
    {
        static Dictionary<string, ClsAlias> aliasDic = new Dictionary<string, ClsAlias>();

        /// <summary>
        /// 可传入文件夹路径或者文件路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        protected static object objHelper = new object();
        public void LoadFromAliasFile(string filePath)
        {

            if (Runtime.IsExistFile(filePath))//传入的是文件路径
            {

                XDocument xd = XDocument.Load(filePath);
                IEnumerable<XElement> xe = xd.Root.Elements();
                foreach (XElement xel in xe)
                {
                    ClsAlias clsAlias = new ClsAlias();
                    clsAlias.AliasName = xel.Attribute("key") == null ? string.Empty : xel.Attribute("key").Value.ToString();
                    clsAlias.RealName = xel.Attribute("value") == null ? string.Empty : xel.Attribute("value").Value.ToString();
                    clsAlias.FilePath = filePath;
                    lock (objHelper)
                    {
                        if (!aliasDic.ContainsKey(clsAlias.AliasName))
                        {
                            aliasDic.Add(clsAlias.AliasName, clsAlias);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 更具别名返回全名
        /// </summary>
        /// <param name="aliasName">别姓</param>
        /// <returns>别名配置的值</returns>
        public ClsAlias GetReal(string aliasName)
        {
            return aliasDic[aliasName];
        }
    }
}
